package com.util;

import com.util.DateTimeUtil;

import java.util.Arrays;
import java.util.Date;

public abstract class StringUtil {
	public static String toUnitHtml(String unit) {
		if (unit == null
				|| unit.isEmpty()) {
			return "<html></html>";
		}
		String ret = "<html>";
		for (int i=0; i<unit.length(); i++) {
			char ch = unit.charAt(i);
			if (ch == '^'
				&& unit.length() > i+2
				&& unit.charAt(i+1) == '[') {
				String right = unit.substring(i+1);
				int index = right.indexOf(']');
				if (index < 0) {
					ret += unit.substring(i);
					break;
				}
				String sup = right.substring(1, index);
				ret += "<sup>" + sup + "</sup>";
				i +=  index + 1;
				
			} else {
				ret += ch;
				continue;
			}
		}
		ret += "</html>";
		return ret;
	}
	/**
	 * 把指定的ascii码字符串转换成16进制Unicode码字符串
	 * */
	public static String toUnicode(String s) { 
		String s1 = ""; 
		for(int i = 0; i < s.length(); i++) { 
		    s1 = s1 + "\\u"+ Integer.toHexString(s.charAt(i) & 0xffff); 
		} 
		return s1; 
	} 

/*	public static Map<String, Object> getMap4Json(String jsonString){
        JSONObject jsonObject = JSONObject.fromObject(jsonString);  
        Iterator<?>  keyIter = jsonObject.keys();
        String key;
        Object value;
        Map<String, Object> valueMap = new HashMap<String, Object>();
        while( keyIter.hasNext()){
          key = (String)keyIter.next();
          value = jsonObject.get(key);
          valueMap.put(key, value);
        }
        return valueMap;
    }
    */
	public static String trimHtml(String text){
    	String result = text;
    	if (result==null) return result;
    	result=result.replace("<html>","");
    	result=result.replace("</html>","");
    	return result;
    }
    
    public static String replaceSup(String text){
    	String result = text;
    	if (result==null) return result;
    	result=result.replace("<sup>","^");
    	result=result.replace("</sup>","");
    	return result;
    }
    
   /* public static int getDisplayWidth(FontMetrics fontMetrics,String string) {
    	return fontMetrics.stringWidth(trimHtml(replaceSup(string)));
    	
    }*/

    public static boolean isEmpty(String data){
    	return data==null||data.trim().length()==0;
    }
    public static boolean notEmpty(String data){
    	return data!=null&&data.trim().length()>0;
    }
    public static String formatHHmm(int hour,int min){
		String hourString=String.valueOf(hour);
		String minString=String.valueOf(min);
		if (hour<10){
			hourString = "0"+String.valueOf(hour);
		}
		if (min<10){
			minString = "0"+String.valueOf(min);
		}
		return hourString+":"+minString;
	}
    
    /**
	 * 判断字符串是否是整数
	 */
	public static boolean isInteger(String value) {
		try {
			Integer.parseInt(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 判断字符串是否是浮点数
	 */
	public static boolean isDouble(String value) {
		try {
			Double.parseDouble(value);
			if (value.contains("."))
				return true;
			return false;
		} catch (NumberFormatException e) {
			return false;
		}
	}

	/**
	 * 判断字符串是否是数字
	 */
	public static boolean isNumber(String value) {
		return isInteger(value) || isDouble(value);
	}
	
	/**
	 * 输出一维数组
	 */
	public static String print(double[] arg1){
		String printStr = "";
		for (int i = 0; i < arg1.length; i++) {
			printStr += arg1[i]+",";
		}
		return printStr.substring(0, printStr.length()-1);
	}
	/**
	 * 输出二维对象数组
	 */
	public static String print(Object[][] arg1){
		String printStr = "";
		for (int i = 0; i < arg1.length; i++) {
			printStr += DateTimeUtil.YYYYMMddHHmmss().format((Date)arg1[i][0])+","+arg1[i][1]+"\n";
		}
		return printStr.substring(0, printStr.length());
	}
	public static boolean toBoolean(String v){
		return "1".equals(v);
	}
	public static void main(String[] args){
		String[] ss="add,".split(",");
		System.out.println(ss.length+" "+Arrays.toString(ss));
	}
}
